package com.gtja.beoms.service.impl;

import com.gtja.beoms.base.CommonConstants;
import com.gtja.beoms.dao.QwCustomerDao;
import com.gtja.beoms.dao.QwGroupDao;
import com.gtja.beoms.dao.QwStaffRelationDao;
import com.gtja.beoms.dto.QwGroupDTO;
import com.gtja.beoms.pojo.QwCustomer;
import com.gtja.beoms.pojo.QwStaffRelation;
import com.gtja.beoms.service.IGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName: GroupChatServiceImpl
 * @Description: 群聊相关逻辑
 * @Author: Li
 * @Date: 2021.05.31
 * @Version: 1.0
 **/
@Service
public class GroupServiceImpl implements IGroupService {

    @Resource
    private QwGroupDao qwGroupDao;

    @Resource
    private QwStaffRelationDao qwStaffRelationDao;

    @Resource
    private QwCustomerDao qwCustomerDao;


    /**
     * @Description: 获取群聊详细信息
     * @Author: Li
     * @Param: [params]
     * @Date: 2021/6/10 16:48
     * @returns: com.gtja.beoms.dto.QwGroupDTO
     */
    @Override
    public QwGroupDTO getGroupInfo(Map<String, Object> params) throws Exception {
        QwGroupDTO qwGroupDTO = qwGroupDao.getGroupInfo(params);
        // 处理成员头像和名字
        if (qwGroupDTO != null) {
            qwGroupDTO.getQwGroupMembers().stream().forEach(m -> {
                if (CommonConstants.GROUP_CUSTOMER_TYPE.equals(String.valueOf(m.getType()))) {
                    // 参数
                    HashMap<String, String> map = new HashMap<>(3);
                    map.put("corpId", (String) params.get("corpId"));
                    map.put("custQwId", m.getUserId());
                    //查询客户信息
                    QwCustomer qwCustomer = qwCustomerDao.getCustom(map);
                    if (qwCustomer != null) {
                        m.setUserHeadUrl(qwCustomer.getCustHeadUrl());
                        m.setUserName(qwCustomer.getCustName());
                    }
                } else if (CommonConstants.GROUP_USER_TYPE.equals(String.valueOf(m.getType()))) {
                    Map<String, String> map = new HashMap<>(3);
                    map.put("corpId", (String) params.get("corpId"));
                    map.put("userQwId", m.getUserId());
                    QwStaffRelation qwStaffRelation = qwStaffRelationDao.getQwStaffRelation(map);
                    if (qwStaffRelation != null) {
                        m.setUserHeadUrl(qwStaffRelation.getUserHeadUrl());
                        m.setUserName(qwStaffRelation.getUserName());
                    }
                }
            });
        }
        return qwGroupDTO;
    }

}
